package cn.com.anypay.manager.common.utils;

import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 业务ID生成器
 * 生成具有业务含义的ID
 */
@Component
public class BusinessIdGenerator {

    private static final AtomicLong SEQUENCE = new AtomicLong(1);
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
    private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    /**
     * 生成订单号
     * 格式：ORD + yyyyMMddHHmmss + 6位序列号
     * @return 订单号
     */
    public static String generateOrderId() {
        String timestamp = LocalDateTime.now().format(DATETIME_FORMATTER);
        long seq = SEQUENCE.getAndIncrement() % 1000000;
        return String.format("ORD%s%06d", timestamp, seq);
    }

    /**
     * 生成支付单号
     * 格式：PAY + yyyyMMddHHmmss + 6位序列号
     * @return 支付单号
     */
    public static String generatePaymentId() {
        String timestamp = LocalDateTime.now().format(DATETIME_FORMATTER);
        long seq = SEQUENCE.getAndIncrement() % 1000000;
        return String.format("PAY%s%06d", timestamp, seq);
    }

    /**
     * 生成退款单号
     * 格式：REF + yyyyMMddHHmmss + 6位序列号
     * @return 退款单号
     */
    public static String generateRefundId() {
        String timestamp = LocalDateTime.now().format(DATETIME_FORMATTER);
        long seq = SEQUENCE.getAndIncrement() % 1000000;
        return String.format("REF%s%06d", timestamp, seq);
    }

    /**
     * 生成二维码ID
     * 生成活码ID (格式: QR+13位时间戳)
     * @return 活码ID
     */
    public static String generateQrcodeId() {
        return "QR" + System.currentTimeMillis();
    }

    /**
     * 生成商户ID (格式: M+13位时间戳)
     * 格式：MCH + yyyyMMdd + 8位序列号
     * @return 商户号
     */
    public static String generateMerchantId() {
        String date = LocalDateTime.now().format(DATE_FORMATTER);
        long seq = SEQUENCE.getAndIncrement() % 100000000;
        return String.format("MCH%s%08d", date, seq);
    }

    /**
     * 生成用户ID
     * 格式：USR + yyyyMMdd + 8位序列号
     * @return 用户ID
     */
    public static String generateUserId() {
        String date = LocalDateTime.now().format(DATE_FORMATTER);
        long seq = SEQUENCE.getAndIncrement() % 100000000;
        return String.format("USR%s%08d", date, seq);
    }

    /**
     * 生成自定义业务ID
     * @param prefix 前缀
     * @param includeTime 是否包含时间戳
     * @param sequenceLength 序列号长度
     * @return 业务ID
     */
    public static String generateCustomId(String prefix, boolean includeTime, int sequenceLength) {
        StringBuilder sb = new StringBuilder(prefix);
        
        if (includeTime) {
            sb.append(LocalDateTime.now().format(DATETIME_FORMATTER));
        }
        
        long maxSeq = (long) Math.pow(10, sequenceLength);
        long seq = SEQUENCE.getAndIncrement() % maxSeq;
        sb.append(String.format("%0" + sequenceLength + "d", seq));
        
        return sb.toString();
    }

    /**
     * 重置序列号（用于测试或特殊场景）
     */
    public static void resetSequence() {
        SEQUENCE.set(1);
    }
}